﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using DataSync.BLL;

namespace DataSync
{
    public partial class FormMain : Form
    {
        private Thread syncThread; //数据同步线程
        private SyncService service;
        private SyncManage syncManage;
        private readonly string exeVersion = ConfigurationManager.AppSettings["ExeVersion"];
        public FormMain()
        {
            InitializeComponent();

            service = new SyncService();
            service.LbMsg = this.lbProgress;
            service.TxtMsg = this.mainStatusStrip;
            this.Text += exeVersion;
            syncManage = new SyncManage();

            syncThread = new Thread(new ThreadStart(service.Run));
            syncThread.IsBackground = true;

            Form_Resize();
        }

        private void FormMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (syncThread.ThreadState != (ThreadState.Unstarted | ThreadState.Background))
            {
                if (MessageBox.Show("数据同步程序正在运行，你确定要退出吗？", "警告", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    e.Cancel = false;
                    syncManage.Log.Info(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " ：" + " 数据同步程序被非正常关闭。");
                }
                else
                {
                    e.Cancel = true;
                }
            }
            else
            {
                syncManage.Log.Info(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " ：" + " 数据同步程序正常退出。");
            }
        }

        private void FormMain_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (syncThread != null)
            {
                if (syncThread.ThreadState == (ThreadState.Background | ThreadState.Suspended))
                {
                    syncThread.Resume();
                }
                syncThread.Abort();
            }

            Application.ExitThread();
            Application.Exit();
        }

        private void FormMain_Load(object sender, EventArgs e)
        {
            //syncThread.Start();
        }

        private void ToolStripMenuItemExit_Click(object sender, EventArgs e)
        {

            if (MessageBox.Show("你确定要退出吗？", "警告", MessageBoxButtons.YesNo) == DialogResult.No)
            {
                return;
            }

            if (syncThread != null)
            {
                if (syncThread.ThreadState == (ThreadState.Background | ThreadState.Suspended))
                {
                    syncThread.Resume();
                }
                syncThread.Abort();
                syncManage.Log.Info(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " ：" + " 数据同步程序正常退出。");
            }

            Application.ExitThread();
            Application.Exit();
        }

        private void ToolStripMenuItemAbout_Click(object sender, EventArgs e)
        {
            new About().ShowDialog();
        }

        private void ToolStripMenuItemCustomize_Click(object sender, EventArgs e)
        {
            new CustomizeSync().Show();
        }

        private void ToolStripMenuItemStartSync_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("你确定同步吗？", "警告", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                if (syncThread.ThreadState == (ThreadState.Unstarted | ThreadState.Background))
                {
                    syncThread.Start();
                    this.mainStatusStrip.Items["toolStripStatusLabel1"].Text = "数据同步程序正在运行";
                    syncManage.Log.Info(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " ：" + " 数据同步程序开始运行…………");
                }
            }
        }

        private void FormMain_Resize(object sender, EventArgs e)
        {
            Form_Resize();
        }

        protected void Form_Resize()
        {
            this.lbProgress.Top = this.menuStrip1.Height + this.toolStrips.Height;
            this.lbProgress.Left = 0;
            this.lbProgress.Height = this.Height - this.toolStrips.Height - this.menuStrip1.Height - this.mainStatusStrip.Height - 25;
        }

        private void lbProgress_DrawItem(object sender, DrawItemEventArgs e)
        {
            string s = this.lbProgress.Items[e.Index].ToString();
            if (s.Contains("未将对象引用到对象实例") || s.Contains("有误") || s.Contains("出错") || s.Contains("异常"))
            {
                e.Graphics.DrawString(s, this.lbProgress.Font, Brushes.Red, e.Bounds);
            }
            else
                e.Graphics.DrawString(s, this.lbProgress.Font, new SolidBrush(this.lbProgress.ForeColor), e.Bounds);
        }
    }
}
